<template>
  <el-dialog
    class="custom-dialog"
    v-model="dialogVisible"
    title="关联承租方"
    width="1200px"
    @close="handleClose"
  >
    <div class="custom-table-list-wrapper">
      <div class="select">
        <span>所在楼层</span>
        <el-checkbox-group v-model="floor">
          <el-checkbox
            v-for="(item, index) of floorsOptions"
            :key="index"
            :label="item.value"
            >{{ item.name }}</el-checkbox
          >
        </el-checkbox-group>
      </div>
      <div class="search" :class="{ active: toggleSearchStatus }">
        <div class="search-left" ref="searchFromRef">
          <el-form class="search-form" inline>
            <el-form-item label="承租类型">
              <el-select
                class="search-select"
                v-model="searchParams.type"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="(item, index) of dictOptions['tenantType']"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="关键词">
              <el-input
                class="search-input"
                v-model="searchParams.name"
                placeholder="请输入产权证号"
                :suffix-icon="Search"
              ></el-input>
            </el-form-item>
          </el-form>
          <a
            class="search-left-toggle"
            v-if="showToggle"
            @click="handleToggleSearch"
          >
            <svg-icon :name="toggleSearchStatus ? 'up' : 'down'" />
            {{ toggleSearchStatus ? '收起' : '展开' }}
          </a>
        </div>
        <div class="search-right">
          <el-button type="primary" @click="initTableListAndSelect(1)"
            ><svg-icon name="search" size="14px" />搜索</el-button
          >
          <el-button @click="handleReset()"
            ><svg-icon name="reset" size="17px" />重置</el-button
          >
        </div>
      </div>
      <div class="content">
        <div class="content-table">
          <el-table
            class="custom-table"
            ref="tableRef"
            :data="tableData"
            @select="handleSelectOne"
            @select-all="handleSelectAll"
          >
            <el-table-column type="selection" width="60" />
            <el-table-column prop="type" label="承租类型">
              <template #default="scope">
                {{ getDictValue('tenantType', scope.row.type) }}</template
              >
            </el-table-column>
            <el-table-column prop="name" label="承租方名称" />
            <el-table-column prop="code" label="承租方代码" />
            <el-table-column prop="businessScope" label="经营范围" />
            <el-table-column prop="remark" label="备注" />
          </el-table>
        </div>
        <div class="content-page">
          <el-pagination
            class="custom-pagination"
            :current-page="paginationParams.pageIndex"
            :page-size="paginationParams.pageSize"
            background
            :page-sizes="[10, 20, 30, 50]"
            layout="total, prev, pager, next, sizes, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="initTableListAndSelect"
          />
        </div>
      </div>
    </div>
    <template #footer>
      <el-button @click="handleClose(false)">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref, onMounted, watch, nextTick } from 'vue'
import { useTableList } from '@/hooks/useTableList'
import { useDict } from '@/hooks/useDict'
import { tenantPageList } from '@/api/manager/dataManage/tenant'
import { ElMessage } from 'element-plus'
const {
  dictOptions,
  initListDictOptions,
  initTreeDictOptions,
  getDictValue,
  getDictStyle,
} = useDict()
const searchParams = ref({
  parkId: '',
  keywords: '',
})
const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  handleReset,
  handleDelete,
  detailRef,
  currentDetail,
  searchFromRef,
} = useTableList(searchParams, tenantPageList)
const props = defineProps({
  parkId: {
    type: String,
  },
  floorsOptions: {
    type: Array,
    default: () => [],
  },
})
const tableRef = ref()
const multipleSelection = ref([])
const floor = ref([])
const emit = defineEmits(['change'])
const dialogVisible = ref(false)
const handleClose = () => {
  floor.value = []
  multipleSelection.value = []
  tableRef.value && tableRef.value.clearSelection()
  dialogVisible.value = false
}
const handleSubmit = () => {
  if (multipleSelection.value.length === 0) {
    ElMessage.warning('请选择承租方')
    return
  }
  if (floor.value.length === 0) {
    ElMessage.warning('请选择所在楼层')
    return
  }
  const datas = []
  multipleSelection.value.forEach((item) => {
    floor.value.forEach((i) => {
      datas.push({ ...item, renterId: item.pkid, floor: i, remark: '' })
    })
  })
  handleClose()
  emit('change', datas)
}
watch(
  () => props.parkId,
  () => {
    if (props.parkId) {
      searchParams.value.parkId = props.parkId
      initTableListAndSelect(1)
    }
  },
  {
    immediate: true,
    deep: true,
  },
)
const handleSelectOne = (selection, row) => {
  // 判断是选中/不选中
  const index = selection.findIndex((i) => i.pkid === row.pkid)
  if (index !== -1) {
    multipleSelection.value.push(row)
  } else {
    multipleSelection.value.splice(index, 1)
  }
}
const handleSelectAll = (selection) => {
  // 判断全选/全不选
  if (selection.length > 0) {
    selection.forEach((row) => {
      const index = multipleSelection.value.findIndex(
        (i) => i.pkid === row.pkid,
      )
      if (index === -1) {
        multipleSelection.value.push(row)
      }
    })
  } else {
    tableData.value.forEach((row) => {
      const index = multipleSelection.value.findIndex(
        (i) => i.pkid === row.pkid,
      )
      if (index !== -1) {
        multipleSelection.value.splice(index, 1)
      }
    })
  }
}
const initTableListAndSelect = async (pageIndex) => {
  await initTableList(pageIndex)
  nextTick(() => {
    if (multipleSelection.value.length > 0) {
      tableData.value.forEach((row) => {
        const isHas = multipleSelection.value.find((i) => i.pkid === row.pkid)
        isHas && tableRef.value && tableRef.value.toggleRowSelection(row, true)
      })
    }
  })
}
defineExpose({ dialogVisible })
onMounted(() => {
  initListDictOptions(['tenantType'])
})
</script>
<style lang="scss" scoped>
@import '@/assets/styles/table.scss';
.select {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  margin-bottom: 10px;
  span {
    color: #333333;
    font-size: 14px;
    margin-right: 10px;
  }
}
.search {
  padding: 0;
  margin: 0;
  box-shadow: none;
  height: auto;
}
.content {
  padding: 0;
}
</style>
